<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<meta name="google-site-verification" content="4b5ChOPlfrWPkfJKVSwbnGC4WDT5fyOTDO62HVBosls">
<meta name="baidu-site-verification" content="iotYLCUzXs">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="git github,">





  <link rel="alternate" href="/atom.xml" title="songjiapo" type="application/atom+xml">






<meta name="description" content="在Windows上安装Git在Windows上使用Git，可以从Git官网直接下载安装程序，（国内镜像），然后按默认选项安装即可。 安装完成后，在开始菜单里找到“Git”-&amp;gt;“Git Bash”，蹦出一个类似命令行窗口的东西，就说明Git安装成功！ 安装完成后，还需要最后一步设置，在命令行输入： 12$ git config --global user.name &amp;quot;Your Nam">
<meta name="keywords" content="git github">
<meta property="og:type" content="article">
<meta property="og:title" content="Git教程">
<meta property="og:url" content="https://songjiapo.com/git/2018-03-17-git_tutorial.html">
<meta property="og:site_name" content="songjiapo">
<meta property="og:description" content="在Windows上安装Git在Windows上使用Git，可以从Git官网直接下载安装程序，（国内镜像），然后按默认选项安装即可。 安装完成后，在开始菜单里找到“Git”-&amp;gt;“Git Bash”，蹦出一个类似命令行窗口的东西，就说明Git安装成功！ 安装完成后，还需要最后一步设置，在命令行输入： 12$ git config --global user.name &amp;quot;Your Nam">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-1-1.png">
<meta property="og:image" content="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-1-1.png">
<meta property="og:image" content="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-2-1.png">
<meta property="og:image" content="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-2-2.png">
<meta property="og:image" content="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/branch.PNG">
<meta property="og:updated_time" content="2019-11-08T12:50:25.673Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git教程">
<meta name="twitter:description" content="在Windows上安装Git在Windows上使用Git，可以从Git官网直接下载安装程序，（国内镜像），然后按默认选项安装即可。 安装完成后，在开始菜单里找到“Git”-&amp;gt;“Git Bash”，蹦出一个类似命令行窗口的东西，就说明Git安装成功！ 安装完成后，还需要最后一步设置，在命令行输入： 12$ git config --global user.name &amp;quot;Your Nam">
<meta name="twitter:image" content="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-1-1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://songjiapo.com/git/2018-03-17-git_tutorial.html">





  <title>Git教程 | songjiapo</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e07193cab6deb965ead8e1aed3d4744f";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">songjiapo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://songjiapo.com/git/2018-03-17-git_tutorial.html">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="song jiapo">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="songjiapo">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Git教程</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-17T16:45:13+08:00">
                2018-03-17
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/git/" itemprop="url" rel="index">
                    <span itemprop="name">git</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/git/2018-03-17-git_tutorial.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/git/2018-03-17-git_tutorial.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="在Windows上安装Git"><a href="#在Windows上安装Git" class="headerlink" title="在Windows上安装Git"></a>在Windows上安装Git</h1><p>在Windows上使用Git，可以从Git官网直接<a href="https://git-scm.com/downloads" target="_blank" rel="noopener">下载安装程序</a>，（<a href="https://pan.baidu.com/s/1kU5OCOB#list/path=%2Fpub%2Fgit" target="_blank" rel="noopener">国内镜像</a>），然后按默认选项安装即可。</p>
<p>安装完成后，在开始菜单里找到“Git”-&gt;“Git Bash”，蹦出一个类似命令行窗口的东西，就说明Git安装成功！</p>
<p>安装完成后，还需要最后一步设置，在命令行输入：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global user.name &quot;Your Name&quot;</span><br><span class="line">$ git config --global user.email &quot;email@example.com&quot;</span><br></pre></td></tr></table></figure>
<h1 id="第一个版本库repository"><a href="#第一个版本库repository" class="headerlink" title="第一个版本库repository"></a>第一个版本库repository</h1><p>什么是版本库呢？版本库又名仓库，英文名repository，你可以简单理解成一个目录，这个目录里面的所有文件都可以被Git管理起来，每个文件的修改、删除，Git都能跟踪，以便任何时刻都可以追踪历史，或者在将来某个时刻可以“还原”。</p>
<p>所以，创建一个版本库非常简单，首先，选择一个合适的地方，创建一个空目录：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir learngit</span><br><span class="line">$ cd learngit</span><br><span class="line">$ pwd</span><br><span class="line">/Users/michael/learngit</span><br></pre></td></tr></table></figure>
<h2 id="初始化一个Git仓库"><a href="#初始化一个Git仓库" class="headerlink" title="初始化一个Git仓库"></a>初始化一个Git仓库</h2><p>通过git init命令把这个目录变成Git可以管理的仓库：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git init</span><br><span class="line">Initialized empty Git repository in /Users/michael/learngit/.git/</span><br></pre></td></tr></table></figure>
<h2 id="把文件添加到版本库"><a href="#把文件添加到版本库" class="headerlink" title="把文件添加到版本库"></a>把文件添加到版本库</h2><h3 id="git-add添加文件"><a href="#git-add添加文件" class="headerlink" title="git add添加文件"></a>git add添加文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git add readme.txt</span><br></pre></td></tr></table></figure>
<h3 id="git-commit提交"><a href="#git-commit提交" class="headerlink" title="git commit提交"></a>git commit提交</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -m &quot;wrote a readme file&quot;</span><br><span class="line">[master (root-commit) cb926e7] wrote a readme file</span><br><span class="line"> 1 file changed, 2 insertions(+)</span><br><span class="line"> create mode 100644 readme.txt</span><br></pre></td></tr></table></figure>
<p>-m 后面输入的是本次提交的说明，可以输入任意内容</p>
<h3 id="工作流程图"><a href="#工作流程图" class="headerlink" title="工作流程图"></a>工作流程图</h3><p>整个上述过程可以被这张 git 官网上的流程图直观地表现:<br><img src="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-1-1.png"><br><img src="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-1-1.png" alt=""></p>
<ul>
<li>图中告诉我们存在4个状态：untracked, unmodified,modified,staged</li>
<li>创建的新文件，且没有git add 则属于untracked状态；add之后为staged状态</li>
<li>staged状态 git commit 之后为 unmodified 状态</li>
<li>当我们修改后，变为modified 状态；需要继续git add 添加，才变为staged状态</li>
<li>当我们git rm之后，变为 untracked状态</li>
</ul>
<h2 id="记录及修改-log-amp-diff"><a href="#记录及修改-log-amp-diff" class="headerlink" title="记录及修改 (log &amp; diff)"></a>记录及修改 (log &amp; diff)</h2><p>在 git 中, 每一次提交(commit)的修改, 都会被单独的保存起来. 也可以说 git 的中的所有文件都是一次次修改累积起来的. 文件好比楼房, 每个 commit 记录 了盖楼需添加或者拿走的材料. 整个施工过程也被记录了下来.</p>
<h3 id="修改记录-log"><a href="#修改记录-log" class="headerlink" title="修改记录 log"></a>修改记录 log</h3><p>我们来查看版本库的施工过程<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git log</span><br></pre></td></tr></table></figure></p>
<h3 id="查看状态status"><a href="#查看状态status" class="headerlink" title="查看状态status"></a>查看状态status</h3><p>git status命令可以让我们时刻掌握仓库当前的状态<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git status</span><br></pre></td></tr></table></figure></p>
<ul>
<li><p>要随时掌握工作区的状态，使用git status命令。</p>
</li>
<li><p>如果git status告诉你有文件被修改过，用git diff可以查看修改内容。</p>
</li>
</ul>
<h3 id="查看-unstaged"><a href="#查看-unstaged" class="headerlink" title="查看 unstaged"></a>查看 unstaged</h3><p>如果想要查看这次还没 add (unstaged) 的修改部分 和上个已经 commit 的文件有何不同, 我们将使用 $ git diff:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git diff</span><br></pre></td></tr></table></figure>
<h3 id="查看-staged-–cached"><a href="#查看-staged-–cached" class="headerlink" title="查看 staged (–cached)"></a>查看 staged (–cached)</h3><p>如果你已经 add 了这次修改, 文件变成了 “可提交状态” (staged), 我们可以在 diff 中添加参数 –cached 来查看修改:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git add .   # add 全部修改的文件</span><br><span class="line">$ git diff --cached</span><br></pre></td></tr></table></figure>
<h1 id="回到从前"><a href="#回到从前" class="headerlink" title="回到从前"></a>回到从前</h1><h2 id="回到从前-reset"><a href="#回到从前-reset" class="headerlink" title="回到从前 (reset)"></a>回到从前 (reset)</h2><h3 id="修改已-commit-的版本"><a href="#修改已-commit-的版本" class="headerlink" title="修改已 commit 的版本"></a>修改已 commit 的版本</h3><p>有时候我们总会忘了什么, 比如已经提交了 commit 却发现在这个 commit 中忘了附上另一个文件。</p>
<p>接下来我们模拟这种情况.我们最后一个 commit 是 change 2, 我们将要添加另外一个文件, 将这个修改也 commit 进 change 2. 所以我们复制 1.py 这个文件, 改名为 2.py. 并把 2.py 变成 staged, 然后使用 <strong>–amend</strong> 将这次改变合并到之前的 change 2 中.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git add 2.py</span><br><span class="line">$ git commit --amend --no-edit   # &quot;--no-edit&quot;: 不编辑, 直接合并到上一个 commit</span><br><span class="line">$ git log --oneline    # &quot;--oneline&quot;: 每个 commit 内容显示在一行</span><br></pre></td></tr></table></figure>
<h3 id="reset-回到-add-之前"><a href="#reset-回到-add-之前" class="headerlink" title="reset 回到 add 之前"></a>reset 回到 add 之前</h3><p>有时我们添加 add 了修改, 但是又后悔, 并想补充一些内容再 add. 这时, 我们有一种方式可以回到 add 之前.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git add 1.py</span><br><span class="line">$ git status</span><br><span class="line">#后悔了 咋办</span><br><span class="line">$ git reset 1.py</span><br></pre></td></tr></table></figure></p>
<h3 id="reset-回到-commit-之前"><a href="#reset-回到-commit-之前" class="headerlink" title="reset 回到 commit 之前"></a>reset 回到 commit 之前</h3><p>在穿梭到过去的 commit 之前, 我们必须了解 git 是如何一步一步累加更改的。<br><img src="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-2-1.png" alt="image"><br><img src="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/2-2-2.png" alt="image"></p>
<p>每个 commit 都有自己的 id 数字号, HEAD 是一个指针, 指引当前的状态是在哪个 commit. 最近的一次 commit 在最右边, 我们如果要回到过去, 就是让 HEAD 回到过去并 reset 此时的 HEAD 到过去的位置.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"># 不管我们之前有没有做了一些 add 工作, 这一步让我们回到 上一次的 commit</span><br><span class="line">$ git reset --hard HEAD    </span><br><span class="line"># 输出</span><br><span class="line">HEAD is now at 904e1ba change 2</span><br><span class="line">-----------------------</span><br><span class="line"># 看看所有的log</span><br><span class="line">$ git log --oneline</span><br><span class="line"># 输出</span><br><span class="line">904e1ba change 2</span><br><span class="line">c6762a1 change 1</span><br><span class="line">13be9a7 create 1.py</span><br><span class="line">-----------------------</span><br><span class="line"># 回到 c10ea64 change 1</span><br><span class="line"># 方式1: &quot;HEAD^&quot;</span><br><span class="line">$ git reset --hard HEAD^  </span><br><span class="line"></span><br><span class="line"># 方式2: &quot;commit id&quot;</span><br><span class="line">$ git reset --hard c6762a1</span><br><span class="line">-----------------------</span><br><span class="line"># 看看现在的 log</span><br><span class="line">$ git log --oneline</span><br><span class="line"># 输出</span><br><span class="line">c6762a1 change 1</span><br><span class="line">13be9a7 create 1.py</span><br></pre></td></tr></table></figure>
<p>怎么 change 2 消失了!!! 还有办法挽救消失的 change 2 吗? 我们可以查看 $ git reflog 里面最近做的所有 HEAD 的改动, 并选择想要挽救的 commit id:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ git reflog</span><br><span class="line"># 输出</span><br><span class="line">c6762a1 HEAD@&#123;0&#125;: reset: moving to c6762a1</span><br><span class="line">904e1ba HEAD@&#123;1&#125;: commit (amend): change 2</span><br><span class="line">0107760 HEAD@&#123;2&#125;: commit: change 2</span><br><span class="line">c6762a1 HEAD@&#123;3&#125;: commit: change 1</span><br><span class="line">13be9a7 HEAD@&#123;4&#125;: commit (initial): create 1.py</span><br></pre></td></tr></table></figure>
<p>重复 reset 步骤就能回到 commit (amend): change 2 (id=904e1ba)这一步了:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ git reset --hard 904e1ba</span><br><span class="line">$ git log --oneline</span><br><span class="line"># 输出</span><br><span class="line">904e1ba change 2</span><br><span class="line">c6762a1 change 1</span><br><span class="line">13be9a7 create 1.py</span><br></pre></td></tr></table></figure>
<p>我们又再次奇迹般的回到了 change 2.</p>
<h2 id="回到从前-checkout-针对单个文件"><a href="#回到从前-checkout-针对单个文件" class="headerlink" title="回到从前 (checkout 针对单个文件)"></a>回到从前 (checkout 针对单个文件)</h2><p>之前我们使用 reset 的时候是针对整个版本库, 回到版本库的某个过去. 不过如果我们只想回到某个文件的过去, 又该怎么办呢?</p>
<h3 id="改写文件-checkout"><a href="#改写文件-checkout" class="headerlink" title="改写文件 checkout"></a>改写文件 checkout</h3><p>我们仅仅要对 1.py 进行回到过去操作, 回到 c6762a1 change 1 这一个 commit.</p>
<p>使用 <strong>checkout + id c6762a1 + – + 文件目录 1.py</strong>, 我们就能将 1.py 的指针 HEAD 放在这个时刻 c6762a1:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ git log --oneline</span><br><span class="line"># 输出</span><br><span class="line">904e1ba change 2</span><br><span class="line">c6762a1 change 1</span><br><span class="line">13be9a7 create 1.py</span><br><span class="line">---------------------</span><br><span class="line">$ git checkout c6762a1 -- 1.py</span><br></pre></td></tr></table></figure>
<h2 id="分支管理"><a href="#分支管理" class="headerlink" title="分支管理"></a>分支管理</h2><h3 id="分支-branch"><a href="#分支-branch" class="headerlink" title="分支 (branch)"></a>分支 (branch)</h3><p>很多时候我们需要给自己或者客户用一个稳定的版本库, 然后同时还在开发另外一个升级版. 自然而然, 我们会想到把这两者分开处理, 用户使用稳定版, 我们开发我们的开发版. 不过 git 的做法却不一样, 它把这两者融合成了一个文件, 使用不同的分支来管理.</p>
<h3 id="分支-图例"><a href="#分支-图例" class="headerlink" title="分支 图例"></a>分支 图例</h3><p>之前我们说编辑的所有改变都是在一条主分支 master 上进行的. 通常我们会把 master 当作最终的版本, 而开发新版本或者新属性的时候, 在另外一个分支上进行, 这样就能使开发和使用互不干扰了<br><img src="https://raw.githubusercontent.com/songapore/For-PicGo/master/img/branch.PNG" alt="image"></p>
<h3 id="使用-branch-创建-dev-分支"><a href="#使用-branch-创建-dev-分支" class="headerlink" title="使用 branch 创建 dev 分支"></a>使用 branch 创建 dev 分支</h3><p>我们之前的文件当中, 仅仅只有一条 master 分支, 我们可以通过 –graph 来观看分支</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ git log --oneline --graph</span><br><span class="line"># 输出</span><br><span class="line">* 47f167e back to change 1 and add comment for 1.py</span><br><span class="line">* 904e1ba change 2</span><br><span class="line">* c6762a1 change 1</span><br><span class="line">* 13be9a7 create 1.py</span><br></pre></td></tr></table></figure>
<p>接着我们建立另一个分支 dev, 并查看所有分支:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ git branch dev    # 建立 dev 分支</span><br><span class="line">$ git branch        # 查看当前分支</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">  dev       </span><br><span class="line">* master    # * 代表了当前的 HEAD 所在的分支</span><br></pre></td></tr></table></figure>
<h3 id="使用-checkout-切换到-dev-分支"><a href="#使用-checkout-切换到-dev-分支" class="headerlink" title="使用 checkout 切换到 dev 分支"></a>使用 checkout 切换到 dev 分支</h3><p>当我们想把 HEAD 切换去 dev 分支的时候, 我们可以用到上次说的 checkout:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout dev</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">Switched to branch &apos;dev&apos;</span><br><span class="line">--------------------------</span><br><span class="line">$ git branch</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">* dev       # 这时 HEAD 已经被切换至 dev 分支</span><br><span class="line">  master</span><br></pre></td></tr></table></figure>
<p>使用 checkout -b + 分支名, 就能直接创建和切换到新建的分支:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout -b  dev</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">Switched to a new branch &apos;dev&apos;</span><br><span class="line">--------------------------</span><br><span class="line">$ git branch</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">* dev       # 这时 HEAD 已经被切换至 dev 分支</span><br><span class="line">  master</span><br></pre></td></tr></table></figure>
<h3 id="在-dev-分支中修改"><a href="#在-dev-分支中修改" class="headerlink" title="在 dev 分支中修改"></a>在 dev 分支中修改</h3><p>因为当前的指针 HEAD 在 dev 分支上, 所以现在对文件夹中的文件进行修改将不会影响到 master 分支.</p>
<p>我们修改dev分支, 然后再 commit:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -am &quot;change 3 in dev&quot;  # &quot;-am&quot;: add 所有改变 并直接 commit</span><br></pre></td></tr></table></figure></p>
<p>假设我们的开发板 dev 已经更新好了, 我们要将 dev 中的修改推送到 master 中, 大家就能使用到正式版中的新功能了.</p>
<p>首先我们要切换到 master, 再将 dev 推送过来.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout master   # 切换至 master 才能把其他分支合并过来</span><br><span class="line"></span><br><span class="line">$ git merge dev         # 将 dev merge 到 master 中</span><br><span class="line">$ git log --oneline --graph</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">* f9584f8 change 3 in dev</span><br><span class="line">* 47f167e back to change 1 and add comment for 1.py</span><br><span class="line">* 904e1ba change 2</span><br><span class="line">* c6762a1 change 1</span><br><span class="line">* 13be9a7 create 1.py</span><br></pre></td></tr></table></figure></p>
<p>要注意的是, 如果直接 git merge dev, git 会采用默认的 Fast forward 格式进行 merge, 这样 merge 的这次操作不会有 commit 信息. log 中也不会有分支的图案. 我们可以采取 –no-ff 这种方式保留 merge 的 commit 信息.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ git merge --no-ff -m &quot;keep merge info&quot; dev         # 保留 merge 信息</span><br><span class="line">$ git log --oneline --graph</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">*   c60668f keep merge info</span><br><span class="line">|\  </span><br><span class="line">| * f9584f8 change 3 in dev         # 这里就能看出, 我们建立过一个分支</span><br><span class="line">|/  </span><br><span class="line">* 47f167e back to change 1 and add comment for 1.py</span><br><span class="line">* 904e1ba change 2</span><br><span class="line">* c6762a1 change 1</span><br><span class="line">* 13be9a7 create 1.py</span><br></pre></td></tr></table></figure></p>
<h2 id="merge-分支冲突"><a href="#merge-分支冲突" class="headerlink" title="merge 分支冲突"></a>merge 分支冲突</h2><p>情况是这样, 想象不仅有人在做开发版 dev 的更新, 还有人在修改 master 中的一些 bug. 当我们再 merge dev 的时候, 冲突就来了. 因为 git 不知道应该怎么处理 merge 时, 在 master 和 dev 的不同修改.</p>
<p>当创建了一个分支后, 我们同时对两个分支都进行了修改.<br>比如在:</p>
<ul>
<li>master 中的 1.py 加上 # edited in master.</li>
<li>dev 中的 1.py 加上 # edited in dev.</li>
</ul>
<p>当我们想要 merge dev 到 master 的时候:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ git branch #查看当前分支</span><br><span class="line"> dev</span><br><span class="line">* master</span><br><span class="line">-------------------------</span><br><span class="line">$ git merge dev  合并</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">Auto-merging 1.py</span><br><span class="line">CONFLICT (content): Merge conflict in 1.py</span><br><span class="line">Automatic merge failed; fix conflicts and then commit the result.</span><br></pre></td></tr></table></figure>
<p>git 发现的我们的 1.py 在 master 和 dev 上的版本是不同的, 所以提示 merge 有冲突. 具体的冲突, git 已经帮我们标记出来, 我们打开 1.py 就能看到:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = 1</span><br><span class="line"># I went back to change 1</span><br><span class="line">&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</span><br><span class="line"># edited in master</span><br><span class="line">=======</span><br><span class="line"># edited in dev</span><br><span class="line">&gt;&gt;&gt;&gt;&gt;&gt;&gt; dev</span><br></pre></td></tr></table></figure></p>
<p>所以我们只要在 1.py 中<strong>手动合并</strong>一下两者的不同就 OK 啦. 然后再 commit 现在的文件, 冲突就解决啦.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -am &quot;solve conflict&quot;</span><br></pre></td></tr></table></figure>
<h3 id="临时修改-stash"><a href="#临时修改-stash" class="headerlink" title="临时修改 (stash)"></a>临时修改 (stash)</h3><h4 id="暂存修改"><a href="#暂存修改" class="headerlink" title="暂存修改"></a>暂存修改</h4><p>假设我们现在在 dev 分支上快乐地改代码:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout dev</span><br></pre></td></tr></table></figure></p>
<p>在 dev 中的 1.py 中加上一行 # feel happy, 然后老板的电话来了, 可是我还没有改进完这些代码. 所以我就用 stash 将这些改变暂时放一边.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">$ git status -s</span><br><span class="line"># 输出</span><br><span class="line"> M 1.py</span><br><span class="line">------------------</span><br><span class="line">$ git stash</span><br><span class="line"># 输出</span><br><span class="line">Saved working directory and index state WIP on dev: f7d2e3a change 3 in dev</span><br><span class="line">HEAD is now at f7d2e3a change 3 in dev</span><br><span class="line">-------------------</span><br><span class="line">$ git status</span><br><span class="line"># 输出</span><br><span class="line">On branch dev</span><br><span class="line">nothing to commit, working directory clean  # 干净得很</span><br></pre></td></tr></table></figure></p>
<h4 id="做其它任务"><a href="#做其它任务" class="headerlink" title="做其它任务"></a>做其它任务</h4><p>然后我们建立另一个 branch 用来完成老板的任务:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout -b boss</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">Switched to a new branch &apos;boss&apos; # 创建并切换到 boss</span><br></pre></td></tr></table></figure></p>
<p>然后苦逼地完成着老板的任务, 比如添加 # lovely boss 去 1.py. 然后 commit, 完成老板的任务.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -am &quot;job from boss&quot;</span><br><span class="line">$ git checkout master</span><br><span class="line">$ git merge --no-ff -m &quot;merged boss job&quot; boss</span><br></pre></td></tr></table></figure></p>
<h4 id="恢复暂存"><a href="#恢复暂存" class="headerlink" title="恢复暂存"></a>恢复暂存</h4><p>轻松了, 现在可以继续开心的在 dev 上刷代码了.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout dev</span><br><span class="line">$ git stash list    # 查看在 stash 中的缓存</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">stash@&#123;0&#125;: WIP on dev: f7d2e3a change 3 in dev</span><br></pre></td></tr></table></figure></p>
<p>上面说明在 dev 中, 我们的确有 stash 的工作. 现在可以通过 pop 来提取这个并继续工作了.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$ git stash pop</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">On branch dev</span><br><span class="line">Changes not staged for commit:</span><br><span class="line">  (use &quot;git add &lt;file&gt;...&quot; to update what will be committed)</span><br><span class="line">  (use &quot;git checkout -- &lt;file&gt;...&quot; to discard changes in working directory)</span><br><span class="line"></span><br><span class="line">	modified:   1.py</span><br><span class="line"></span><br><span class="line">no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;)</span><br><span class="line">Dropped refs/stash@&#123;0&#125; (23332b7edc105a579b09b127336240a45756a91c)</span><br><span class="line">----------------------</span><br><span class="line">$ git status -s</span><br><span class="line"># 输出</span><br><span class="line"> M 1.py     # 和最开始一样了</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/git-github/" rel="tag"># git github</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/git/2018-03-16-gitignore.html" rel="next" title="Git忽略规则及.gitignore规则正确姿势">
                <i class="fa fa-chevron-left"></i> Git忽略规则及.gitignore规则正确姿势
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/ros_tutorial/2018-03-20-ROS教程-0 目录-《用ROS学习机器人编程的系统方法》.html" rel="prev" title="ROS教程-1.1 目录-《用ROS学习机器人编程的系统方法》">
                ROS教程-1.1 目录-《用ROS学习机器人编程的系统方法》 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">song jiapo</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">57</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/songapore" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:291946540@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#在Windows上安装Git"><span class="nav-number">1.</span> <span class="nav-text">在Windows上安装Git</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#第一个版本库repository"><span class="nav-number">2.</span> <span class="nav-text">第一个版本库repository</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#初始化一个Git仓库"><span class="nav-number">2.1.</span> <span class="nav-text">初始化一个Git仓库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#把文件添加到版本库"><span class="nav-number">2.2.</span> <span class="nav-text">把文件添加到版本库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#git-add添加文件"><span class="nav-number">2.2.1.</span> <span class="nav-text">git add添加文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#git-commit提交"><span class="nav-number">2.2.2.</span> <span class="nav-text">git commit提交</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#工作流程图"><span class="nav-number">2.2.3.</span> <span class="nav-text">工作流程图</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#记录及修改-log-amp-diff"><span class="nav-number">2.3.</span> <span class="nav-text">记录及修改 (log &amp; diff)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#修改记录-log"><span class="nav-number">2.3.1.</span> <span class="nav-text">修改记录 log</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看状态status"><span class="nav-number">2.3.2.</span> <span class="nav-text">查看状态status</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看-unstaged"><span class="nav-number">2.3.3.</span> <span class="nav-text">查看 unstaged</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看-staged-–cached"><span class="nav-number">2.3.4.</span> <span class="nav-text">查看 staged (–cached)</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#回到从前"><span class="nav-number">3.</span> <span class="nav-text">回到从前</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#回到从前-reset"><span class="nav-number">3.1.</span> <span class="nav-text">回到从前 (reset)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#修改已-commit-的版本"><span class="nav-number">3.1.1.</span> <span class="nav-text">修改已 commit 的版本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#reset-回到-add-之前"><span class="nav-number">3.1.2.</span> <span class="nav-text">reset 回到 add 之前</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#reset-回到-commit-之前"><span class="nav-number">3.1.3.</span> <span class="nav-text">reset 回到 commit 之前</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#回到从前-checkout-针对单个文件"><span class="nav-number">3.2.</span> <span class="nav-text">回到从前 (checkout 针对单个文件)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#改写文件-checkout"><span class="nav-number">3.2.1.</span> <span class="nav-text">改写文件 checkout</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分支管理"><span class="nav-number">3.3.</span> <span class="nav-text">分支管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#分支-branch"><span class="nav-number">3.3.1.</span> <span class="nav-text">分支 (branch)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分支-图例"><span class="nav-number">3.3.2.</span> <span class="nav-text">分支 图例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-branch-创建-dev-分支"><span class="nav-number">3.3.3.</span> <span class="nav-text">使用 branch 创建 dev 分支</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-checkout-切换到-dev-分支"><span class="nav-number">3.3.4.</span> <span class="nav-text">使用 checkout 切换到 dev 分支</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在-dev-分支中修改"><span class="nav-number">3.3.5.</span> <span class="nav-text">在 dev 分支中修改</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#merge-分支冲突"><span class="nav-number">3.4.</span> <span class="nav-text">merge 分支冲突</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#临时修改-stash"><span class="nav-number">3.4.1.</span> <span class="nav-text">临时修改 (stash)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#暂存修改"><span class="nav-number">3.4.1.1.</span> <span class="nav-text">暂存修改</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#做其它任务"><span class="nav-number">3.4.1.2.</span> <span class="nav-text">做其它任务</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#恢复暂存"><span class="nav-number">3.4.1.3.</span> <span class="nav-text">恢复暂存</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">song jiapo</span>

  
</div>









        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'l1gitUPNjT1yErmRT7fmIUb3-gzGzoHsz',
        appKey: 'cJ2gj9m4cWlnD3Yw8VBXz3pw',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  


  

  

</body>
</html>
